

;===============
;=             =
;= IM2 HANDLER =
;=             =
;===============
;
;	im2handler     --- (interrupt)
;	add_plugin     DE=plugin address
;	remove_plugin  DE=plugin address
;


	.MODULE im2handler

im2handler
	push af		;store registers
	push bc
	push de
	push hl
	exx
	push bc
	push de
	push hl
	exa
	push af
	push ix
	push iy

	ld hl, (timer)	;update timer
	ld de, 1
	add hl, de
	ld a, (timer+2)
	adc a, 0
  ld (timer), hl
	ld (timer+2), a

	ld a, r		;semaphor for sound routine
	and 127



	ld hl, _plugin
_next_plug
	ld e, (hl)	;read adress of plugin
	inc hl
	ld d, (hl)
	inc hl
	ld a, d
	or e
	jr z, _end_plug	;0x0000 indicates end of plugins
	ld (_plug_call + 1), de			
			;modify plugin call
	push hl
_plug_call
	call 0		;call plugin
	pop hl
	jr _next_plug	
_end_plug


	
	pop iy		;restore registers
	pop ix
	pop af
	exa
	pop hl
	pop de
	pop bc
	exx
	pop hl
	pop de
	pop bc
	pop af
	ei
	ret

add_plugin
	;puts plugin into stack on first available place
	;DE = plugin
	di
	ld hl, _plugin
_find_add
	ld a, (hl)
	inc hl	
	or (hl)
	jr z, _add_here
	inc hl
	jr _find_add	
_add_here
	ld (hl), d
	dec hl
	ld (hl), e
	ei
	ret


remove_plugin
	;remove plugin
	;DE = plugin

	di
	ld hl, _plugin
_remove_next
	ld a, (hl)
	inc hl
	ld c, (hl)
	inc hl
	
	cp e
	jr nz, _remove_next
	ld a, c
	cp d
	jr nz, _remove_next

	dec hl
	xor a
	ld (hl), a
	dec hl
	ld (hl), a

	ex de, hl
	ld hl, 2
	add hl, de
	ld bc, 6
	ldir
	ei
	ret

timer	.BYTE 0,0,0

_plugin	.WORD 0, 0, 0, 0, 0, 0, 0	;up to four widgets